<?php
class BrandsController extends AppController {
    var $name = 'Brands';

    var $pageTitle = 'Brand Description';

    var $viewPath = 'its/inventories/brands';

    var $uses = array('Brand', 'Product');

    var $helpers = array('Html', 'Form', 'Ajax', 'My');

    function beforeFilter() {
        parent::beforeFilter();
        $this->set('parentNav', 'its_admins');
        $this->set('tabMenu', 'its/ItsInventories/tabMenus');
        $this->set('tabMenu', 'tabmenus/its/admins');
    }

    function display() {
        parent::display();
        $this->_paging(Inflector::classify($this->name), array('name' => 'LIKE', 'supplier_id' => '='));
    }

    function save() {
        $view = 'view';
        $brand_id = null;
        if (!empty($this->data)) {
            $this->data['Brand']['collector_id'] = $this->Session->read('Auth.User.id');
            if($this->Brand->save($this->data)) {
                $this->Session->setFlash("Brand {$this->data['Brand']['name']} save succesfull.", 'flash/ok');
                $brand_id = $this->Brand->id;
            } else {
                $this->Session->setFlash("Brand save fail.", 'flash/fail');
                // ถ้าเป็น mode edit เก็บ id เอาไว้กรณีบันทึกข้อมูลผิด
                $brand_id = $this->data['Brand']['id'];
            }
        }
        $this->setAction('view', $brand_id);
    }

    function view($id = null) {
        // edit
        if ($id != null) {
            $this->data = $this->Brand->findById($id);

            $this->Brand->id = $id;
            $this->set('neighbors', $this->Brand->find('neighbors', array('fields' => array('id', 'name'))));
            $this->set('mode', 'edit');
        } else {
            $this->set('mode', 'add');
        }

        $this->set('suppliers', $this->Brand->Supplier->find('list', array('field', array('Supplier.id', 'Supplier.name'))));
        $this->render('view');
    }

    function delete($id = null) {
        $is_success = false;

        // ถ้ายังไม่มี Product ไหน ใช้ Brandนี้ ลบได้
        if(!$this->Product->hasAny(array('brand_id' => $id))) {
            $is_success = $this->Brand->delete($id);
        }

        if ($is_success) {
            $this->Session->setFlash("Brand delete succesfull.", 'flash/ok');
            $this->setAction('display');
        } else {
            $this->Session->setFlash("Can not delete this brand.", 'flash/fail');
            $this->setAction('view', $id);
        }
    }

    function inplace_editor($field, $id) {
        parent::inplace_editor($this->Brand, $field, $id);
    }

    function popup_search() {
        parent::popup_search();
        $this->set('suppliers', $this->Brand->Supplier->find('list', array('field', array('Supplier.id', 'Supplier.name'))));
    }

    function option_list($supplier_id) {
        $brands = $this->Brand->find('list', array('conditions' => array('supplier_id' => $supplier_id)));
        parent::option_list($brands);
    }
}
?>
